<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="名称">
          <a-input placeholder="请输入名称" v-decorator="['name', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="编码">
          <a-input placeholder="请输入编码" v-decorator="['code', {}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="图片">
          <div class="clearfix">
            <a-upload
              listType="picture-card"
              :fileList="fileList"
              :headers="headers"
              :action="uploadActions"
              @preview="handlePreview"
              @change="handleChange"
            >
              <div v-if="fileList.length < 5">
                <a-icon type="plus"/>
                <div class="ant-upload-text">Upload</div>
              </div>
            </a-upload>
            <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancelImg">
              <img alt="example" style="width: 100%;height: 100%" :src="previewImage"/>
            </a-modal>
          </div>
        </a-form-item>


        <a-form-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="uploadAction"
            :data="{'isup':1}"
            :multiple="true"
            :headers="headers"
            :beforeUpload="beforeUpload"
            @change="handleChange1"
          >
            <img v-if="picUrl" :src="getAvatarView()" alt="图片" style="height:104px;max-width:300px"/>
            <div v-else v-decorator="['cover', validatorRules.cover]">
              <a-icon :type="uploadLoading ? 'loading' : 'plus'"/>
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import Vue from 'vue'
  import { ACCESS_TOKEN } from '@/store/mutation-types'
  import moment from 'moment'

  export default {
    name: 'sysDemoTestModal',
    data() {
      return {
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: '/project/sysDemoTest/add',
          edit: '/project/sysDemoTest/edit',
          // fileUpload: window._CONFIG['domianURL']+"/base/fileInfo/uploadImg",
          fileUpload: window._CONFIG['domianURL'] + '/sys/common/upload',
          fileUploads: window._CONFIG['domianURL'] + '/sys/common/upload'
        },
        //
        previewVisible: false,
        previewImage: '',
        fileList: [/*{
          uid: '-1',
          name: 'xxx.png',
          status: 'done',
          url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png'
        }*/],
        formItemLayout: {
          labelCol: { span: 6 },
          wrapperCol: { span: 14 }
        },

        headers: { 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN) },
        // :headers="tokenHeader"
        picUrl: '',
        picUrls: '',
        cover: { rules: [{ required: true, message: '请上传图片!' }] },
        uploadLoading: false,
        uploadLoadings: false
      }
    },
    created() {
    },
    computed: {
      uploadAction: function() {
        return this.url.fileUpload
      },
      uploadActions: function() {
        return this.url.fileUploads
      }
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'name', 'code'))
          //时间格式化
        })

      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            //时间格式化

            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })


          }
        })
      },
      handleCancel() {
        this.close()
      },

      //
      handleCancelImg() {
        this.previewVisible = false
      },
      handlePreview(file) {
        this.previewImage = file.url || file.thumbUrl
        this.previewVisible = true
      },
      handleChange({ fileList }) {
        this.fileList = fileList


        /*  this.info = info
          console.log('handleChange')
          console.log(info)

          var res = info.file.response
          console.log('res')
          console.log(res)

          this.picUrls = ''
          if (info.file.status === 'uploading') {
            this.uploadLoadings = true
            return
          }
          if (info.file.status === 'done') {
            var response = info.file.response
            this.uploadLoadings = false
            console.log(response)
            if (response.success) {
              // this.model.cover = response.result;
              console.log('response:')
              console.log(response)
              console.log('cover:')
              // console.log( this.model.cover)
              this.picUrl = 'Has no pic url yet'
            } else {
              this.$message.warning(response.message)
            }
          }*/
      },
      /*  normFile(e) {
          console.log('Upload event:', e)
          if (Array.isArray(e)) {
            return e
          }
          return e && e.fileList
        },*/


      beforeUpload: function(file) {
        var fileType = file.type
        if (fileType.indexOf('image') < 0) {
          this.$message.warning('请上传图片')
          return false
        }
        //TODO 验证文件大小
      },
      handleChange1(info) {
        console.log('handleChange1:')
        console.log(info)
        this.picUrl = ''
        if (info.file.status === 'uploading') {
          this.uploadLoading = true
          return
        }
        if (info.file.status === 'done') {
          var response = info.file.response
          this.uploadLoading = false
          if (response.success) {
            // this.model.cover = response.result;
            this.model.cover = response.message
            console.log('response:')
            console.log(response)
            console.log('cover:')
            console.log(this.model.cover)

            this.picUrl = 'Has no pic url yet'
          } else {
            this.$message.warning(response.message)
          }
        }
      },
        // nohup java -jar -Xms1024m -Xmx1024m jeecg-boot-module-system-2.1.3-20200108.jar --spring.profiles.active=prod >catalina.out 2>&1 &

      getAvatarView() {
        return `${window._CONFIG['imgPrefix']}` + '/' + this.model.cover
      }
    }
  }
</script>

<style lang="less" scoped>
  /* .ant-upload-select-picture-card i {
     font-size: 32px;
     color: #999;
   }

   .ant-upload-select-picture-card .ant-upload-text {
     margin-top: 8px;
     color: #666;
   }*/
</style>